package moodish.dummy;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;
import javax.swing.*;

import moodish.client.MoodishClient;


public class ClientMainDummy {
	
    public static ClientCommDummy clientComm;
    public static MoodishClient  client;
    private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    private static JFrame janelaConnect;
    public static JTextField serverAddress, nickname;
	public static void main(String[] args) {
            double width = screenSize.getWidth();
            double height = screenSize.getHeight();
            
            janelaConnect = new JFrame("Moodish");
 
            janelaConnect.setSize(400,100);
            janelaConnect.setLocation((int) (width/2 - 200), (int) (height/2-50));
            
            Container cp = janelaConnect.getContentPane();
            cp.setLayout(new FlowLayout());
            
            JLabel urlLabel = new JLabel("URL: ");
            JLabel nickLabel = new JLabel("Nickname: ");
            
            serverAddress = new JTextField(29);
            nickname = new JTextField(18);
           
            JButton connect = new JButton("Connect");
            connect.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    clientComm = new ClientCommDummy();
                    client     = new MoodishClientDummy();
                    if(serverAddress.getText().isEmpty())
                    	serverAddress.setText("localhost");
                	if(nickname.getText().isEmpty()){
                		JOptionPane.showMessageDialog(null,
								"Nickname can't be empty!",
								"Error!", JOptionPane.INFORMATION_MESSAGE);
                	} else {
	                    try {
	                        clientComm.connect(serverAddress.getText(), nickname.getText());
	                        client.start(clientComm);
		                    janelaConnect.setVisible(false);
		                    System.out.println("Connected with nickname: "+nickname.getText());
	                    } catch (UnknownHostException ex) {
	                    	JOptionPane.showMessageDialog(null,
									"Unknown Host!",
									"Connection Error!", JOptionPane.INFORMATION_MESSAGE);
	                    } catch (IOException ex) {
	                    	JOptionPane.showMessageDialog(null,
									"I/O Error!",
									"Connection Error!", JOptionPane.INFORMATION_MESSAGE);
	                    }
                	}
                }
            });
            
            cp.add(urlLabel);
            cp.add(serverAddress);
            cp.add(nickLabel);
            cp.add(nickname);
            cp.add(connect);
            
            janelaConnect.setVisible(true);
            janelaConnect.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            
	}
}
